<template>
  <a-modal v-model="visible" :title="title" :confirmLoading="submitLoading" width="600px" ok-text="确认" cancel-text="取消" @ok="addFormSubmit">
    <a-form :form="events_form" :label-col="{ span: 8 }" :wrapper-col="{ span: 12 }">
      <a-form-item label="挂靠人员姓名" v-if="insureNo">
        {{ info.insureStaffName }}
      </a-form-item>
      <a-form-item label="参保方案选择" v-if="insureNo">
        <a-select placeholder="请选择" v-decorator="['caseId']">
          <a-select-option :key="item.caseId" :value="item.caseId" v-for="item in planData">{{ item.caseName }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="养老缴费基数">
        <a-input placeholder="请输入养老缴费基数" v-decorator="['pensionBase']"></a-input>
      </a-form-item>
      <a-form-item label="医疗缴费基数">
        <a-input placeholder="请输入医疗缴费基数" v-decorator="['medicalBase']"></a-input>
      </a-form-item>
      <a-form-item label="工伤缴费基数">
        <a-input placeholder="请输入工伤缴费基数" v-decorator="['injuryBase']"></a-input>
      </a-form-item>
      <a-form-item label="失业缴费基数">
        <a-input placeholder="请输入失业缴费基数" v-decorator="['loseJobBase']"></a-input>
      </a-form-item>
      <a-form-item label="生育缴费基数">
        <a-input placeholder="请输入生育缴费基数" v-decorator="['birthBase']"></a-input>
      </a-form-item>
      <a-form-item label="大病缴费基数">
        <a-input placeholder="请输入生育缴费基数" v-decorator="['bigIllBase']"></a-input>
      </a-form-item>
      <a-form-item label="公积金缴费基数">
        <a-input placeholder="请输入公积金缴费基数" v-decorator="['fundsBase']"></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import {
  queryInsureStaffBaseInfo,
  adjustInsureBase,
  updateInsureBase
} from "@/api/human/ensureJoinStaff"
import store from '@/store'
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'AddInfo',
  data() {
    return {
      title: "",
      visible: false,
      submitLoading: false,

      insureNo: "",
      insureNoList: [],
      info: {},
      caseId: undefined,
      planData: [],
    }
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    });
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    // 打开编辑弹窗 
    async add(row, list) {
      this.visible = true;
      this.insureNo = row === true ? '' : row.insureNo;
      if (row == true) {
        this.title = '一键调基';
        this.$nextTick(() => {
          this.events_form.resetFields();
        })
        if (list.length > 0) {
          this.insureNoList = list
        }
      } else {
        this.title = '修改基数';
        if (list.length > 0) {
          this.planData = list
        }
        let { code, data } = await queryInsureStaffBaseInfo(this.insureNo)
        let values = this.events_form.getFieldsValue();
        if (code == '1') {
          this.info = data;
          for(var key in data){
            values[key] = data[key]
          }
          this.events_form.setFieldsValue(values);
        }
      }
    },
    // 提交
    async addFormSubmit() {
      this.events_form.validateFieldsAndScroll(async (err, values) => {
        if (!err) {
          let params = {};
          if (this.insureNo) {
            // 修改基数
            values.insureNo = this.insureNo;
            params = values;
          } else {
            // 一键调基
            params.insureNoList = this.insureNoList;
            params.insureAdjustBaseDto = values
          }
          this.submitLoading = true;
          const { code } = this.insureNo ? await updateInsureBase(params): await adjustInsureBase(params);
          this.submitLoading = false;
          if (code === '1') {
            this.visible = false;
            this.$message.success('操作成功');
            this.$emit('on-ok');
          }
        }
      });
    }
  }
}
</script>
